package com.jrx.anytxn.transaction.job.autoPayment.config;

import com.jrx.anytxn.param.entity.PrOrganizationTable;
import com.jrx.anytxn.param.service.system.IOrganizationTableService;
import com.jrx.anytxn.transaction.config.AnytxnBatchProperties;
import com.jrx.anytxn.transaction.constant.TransBizConstant;
import com.jrx.anytxn.transaction.entity.BtAutoPaymentList;
import com.jrx.anytxn.transaction.entity.TlPaymentLog;
import com.jrx.anytxn.transaction.job.autoPayment.step.*;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.item.ItemReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.transaction.PlatformTransactionManager;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 批扣第二次初始化
 *
 * @author : zhenghaopu
 * 2019/11/22
 */
@Configuration
@EnableConfigurationProperties(AnytxnBatchProperties.class)
public class AutoPaymentSecondInitConfiguration {
    @Resource(name = "bizSqlSessionFactory")
    SqlSessionFactory sqlSessionFactory;

    @Autowired
    public JobBuilderFactory jobBuilderFactory;

    @Autowired
    public StepBuilderFactory stepBuilderFactory;


    @Autowired
    private AnytxnBatchProperties anytxnBatchProperties;

    @Autowired
    private IOrganizationTableService organizationTableService;
    @Autowired
    private AutoPaymentSecondInitProcessor autoPaymentSecondInitProcessor;
    @Autowired
    private AutoPaymentSecondInitWriter autoPaymentSecondInitWriter;
    @Autowired
    private AutoPaymentInitListener autoPaymentInitListener;
    @Autowired
    private PlatformTransactionManager platformTransactionManager;
    @Autowired
    private AutoPaymentSecondInitProcessor2 autoPaymentSecondInitProcessor2;

    @Bean
    public Job autoPaymentSecondInitJob(){
        return jobBuilderFactory.get("autoPaymentSecondInitJob")
                .listener(autoPaymentInitListener)//清空批次表
                .start(autoPaymentSecondInitStep())//获取上次批扣失败的流水
                .next(autoPaymentSecondInitStep2())//获取上次非足额扣款成功的流水
                .build();
    }

    @Bean
    public Step autoPaymentSecondInitStep(){
        return stepBuilderFactory.get("autoPaymentSecondInitStep")
                .<TlPaymentLog, BtAutoPaymentList>chunk(anytxnBatchProperties.getAutoPaymentInitConfiguration().getChunkLimit())
                //表示一批处理100
                // 条记录，数据是逐条发送给processor处理的，处理100条记录后，统一返回给writer进行保存处理
                .reader(autoPaymentSecondInitReader())
                .processor(autoPaymentSecondInitProcessor)
                .writer(autoPaymentSecondInitWriter)
                .transactionManager(platformTransactionManager)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anytxnBatchProperties.getAutoPaymentInitConfiguration().getThrottleLimit())//多线程*/
                .build();
    }


    @Bean
    @StepScope
    public ItemReader<TlPaymentLog> autoPaymentSecondInitReader() {

        PrOrganizationTable org = organizationTableService.findByOrgIdForBatch(TransBizConstant.DEFAULT_ORG_ID);
        Date businessDate = org.getNextProcessingDate();
        return new AutoPaymentSecondInitReader(businessDate,sqlSessionFactory,
                anytxnBatchProperties.getAutoPaymentInitConfiguration().getPageSize());
    }

    @Bean
    public Step autoPaymentSecondInitStep2(){
        return stepBuilderFactory.get("autoPaymentSecondInitStep2")
                .<TlPaymentLog, BtAutoPaymentList>chunk(anytxnBatchProperties.getAutoPaymentInitConfiguration().getChunkLimit())
                //表示一批处理100
                // 条记录，数据是逐条发送给processor处理的，处理100条记录后，统一返回给writer进行保存处理
                .reader(autoPaymentSecondInitReader2())
                .processor(autoPaymentSecondInitProcessor2)
                .writer(autoPaymentSecondInitWriter)
                .transactionManager(platformTransactionManager)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anytxnBatchProperties.getAutoPaymentInitConfiguration().getThrottleLimit())//多线程*/
                .build();
    }

    @Bean
    @StepScope
    public ItemReader<TlPaymentLog> autoPaymentSecondInitReader2() {
        PrOrganizationTable org = organizationTableService.findByOrgIdForBatch(TransBizConstant.DEFAULT_ORG_ID);
        Date businessDate = org.getNextProcessingDate();
        return new AutoPaymentSecondInitReader2(businessDate,sqlSessionFactory,
                anytxnBatchProperties.getAutoPaymentInitConfiguration().getPageSize());
    }
}
